package com.oracle.lingshangejia.activity.special;

import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.utils.common.AdapterIntegerDefault0;
import com.lws.basic.utils.common.DoubleDefault0Adapter;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.ErTongZhongYiBean;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//儿童中医详情
public class ErTongZongYiDetailActivity extends BaseActivity {


    @BindView(R.id.tv_yl)
    TextView mTvYl;
    @BindView(R.id.tv_zyyjk)
    TextView mTvZyyjk;
    @BindView(R.id.tv_date)
    TextView mTvDate;

    @Override
    public int setLayoutId() {
        return R.layout.activity_er_tong_zong_yi_detail;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        getData();
    }

    private void getData(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<ErTongZhongYiBean>() {
            @Override
            public void subscribe(ObservableEmitter<ErTongZhongYiBean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkgoNetwork.post(Constant.er_tong_zhong_yi_detail,jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, ErTongZhongYiBean.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<ErTongZhongYiBean>() {
            @Override
            public void onNext(ErTongZhongYiBean bean) {
                try {


                    dismissLoading();
                    if (bean.getData() != null) {
                        mTvDate.setText("时间：" + bean.getData().getCreateDate());
                        mTvYl.setText("月龄：" + yl(bean.getData().getYl()));
                        mTvZyyjk.setText(zyyjk(bean.getData().getYl(), bean.getData().getZyyjkglfw(), bean.getData().getZyyjkglfwQt()));
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //返回月龄
    private String yl(int index){
        String yl="";
        switch (index){
            case 1:
                yl ="六月";
                break;
            case 2:
                yl ="十二月";
                break;
            case 3:
                yl ="十八月";
                break;
            case 4:
                yl ="二十四月";
                break;
            case 5:
                yl ="三十月";
                break;
            case 6:
                yl ="三十六月";
                break;

        }
        return yl;

    }

    //中医药健康
    private String zyyjk(int yl, List<String> list,String qt){
        String zyyjk ="";
        for (int i=0; i<list.size() ;i++){
             switch (list.get(i)){
                 case "1":
                     zyyjk +="中医饮食调养指导 ";
                     break;
                 case "2":
                     zyyjk +="中医起居调摄指导 ";
                     break;
                 case "3":
                     switch (yl){
                         case 1:
                         case 2:
                             zyyjk +="传授摩腹、捏脊方法 ";
                             break;
                         case 3:
                         case 4:
                             zyyjk +="传授按揉迎香穴、足三里穴方法 ";
                             break;
                         case 5:
                         case 6:
                             zyyjk +="传授按揉四神聪穴方法 ";
                             break;
                     }
                     break;
                 case "4":
                     zyyjk +=qt;
                     break;
             }
        }
        return zyyjk;

    }

}
